package god.dream.loginfo.controller;

import god.dream.loginfo.model.LogInfo;
import god.dream.loginfo.service.LogInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class SignonController extends MultiActionController {
	@Autowired
	private LogInfoService logInfoService;
	
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String loginId = request.getParameter("loginId");
		String loginPw = request.getParameter("loginPw");
		
		try {
			if(loginId == null)
				return new ModelAndView("login");
			if( logInfoService.login(loginId, loginPw)) {
				LogInfo logInfo = logInfoService.findLogInfoWithLoginId(loginId);
				request.getSession().setAttribute("logInfo", logInfo);				
				return new ModelAndView("redirect:/cont/listUsers.htm");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new ModelAndView("login");
	}
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.getSession().removeAttribute("logInfo");

		return new ModelAndView("login");
	}
	public LogInfoService getLogInfoService() {
		return logInfoService;
	}
	public void setLogInfoService(LogInfoService logInfoService) {
		this.logInfoService = logInfoService;
	}
}
